<template>
  <div class="app-container">

    <div class="custom-icon-text" style="margin-top: -15px">
      <div class="search-form" style="width: 100%">
        <div style="width: 60px;height: 60px;margin-left: 10px;margin-top: 0px">
          <el-button icon="el-icon-s-data" class="custom-button">总数<br><br>{{total}}</el-button>
        </div>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" style="margin-top: 10px" label-width="68px">
          <div style=" border-bottom: 1px solid #333;margin-bottom: 10px">
            <div style="margin-left: 450px;margin-top: -60px">
              <el-form-item>
                <el-input v-model="queryParams.unitParams" style="width: 220px" clearable placeholder="请输入单位名称/地址"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" style="margin-left: -11px"  @click="handleQuery"></el-button>
                <el-button icon="el-icon-refresh" size="mini"@click="advancedSearch">高级检索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置条件</el-button>
              </el-form-item>
            </div>
          </div>
          <div v-if="isAdvancedSearch" style="width: 100%;margin-top: 5px">
            <el-form-item label="所属辖区">
              <el-cascader
                v-model="areasIds"
                clearable
                :options="areasList"
                :props="{ expandTrigger: 'click' ,value: 'id', label: 'name', children: 'childrenList'}"
                @change="aressChange">
              </el-cascader>
            </el-form-item>
            <el-form-item label="小区名称" prop="plotId">
              <el-select v-model="queryParams.plotId" placeholder="请选择小区" clearable>
                <el-option v-for="i in plotList" :key="i.id" :label="i.plotName" :value="i.id"></el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:units:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:units:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:units:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:units:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="unitsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" width="50">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位图片" align="center" prop="unitPicture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.unitPicture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="center" prop="unitName" />
      <el-table-column label="类别名称" align="center" prop="categoryName" />
      <el-table-column label="小区名称" align="center" prop="plotName" />
      <el-table-column label="单位地址" align="center" prop="unitAddress" />
      <el-table-column label="所属辖区" align="center" prop="areasName" />
      <el-table-column label="从业人员数量" align="center" prop="staffCount" />
      <el-table-column label="联系人姓名" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="证件号码" align="center" prop="idNumber" />
<!--      <el-table-column label="单位坐标点" align="center" prop="locationCoordinates" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:units:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:units:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实有单位ID对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位图片" prop="unitPicture">
          <image-upload v-model="form.unitPicture"></image-upload>
        </el-form-item>
        <el-form-item label="单位名称" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="单位类别名称" prop="categoryId">
          <el-input v-model="form.name" placeholder="请输入单位类别ID，外键关联smart_unit_categories表" />
        </el-form-item>
        <el-form-item label="所属小区名称" prop="plotId">
          <el-input v-model="form.plotName" placeholder="请输入所属小区ID，用于关联小区信息" />
        </el-form-item>
        <el-form-item label="单位地址" prop="unitAddress">
          <el-input v-model="form.unitAddress" placeholder="请输入单位地址" />
        </el-form-item>
        <el-form-item label="从业人员数量" prop="staffCount">
          <el-input v-model="form.staffCount" placeholder="请输入从业人员数量" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="证件号码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入证件号码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUnits, getUnits, delUnits, addUnits, updateUnits } from "@/api/system/units";
import {listPlots} from "@/api/system/smart/plot";
import {threeAreasList} from "@/api/system/smart/estate";

export default {
  name: "Units",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实有单位ID表格数据
      unitsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitParams: null,
        areasId: null,
        plotId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        unitName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "单位类别ID，外键关联smart_unit_categories表不能为空", trigger: "blur" }
        ],
        plotId: [
          { required: true, message: "所属小区ID，用于关联小区信息不能为空", trigger: "blur" }
        ],
        unitAddress: [
          { required: true, message: "单位地址不能为空", trigger: "blur" }
        ],
        staffCount: [
          { required: true, message: "从业人员数量不能为空", trigger: "blur" }
        ],
      },
      areasIds: [],
      areasList: {},
      plotList: {},
      userId: 0,
      user: {},
      isAdvancedSearch: false
    };
  },
  created() {
    this.user = JSON.parse(sessionStorage.getItem('user'))
    this.getList();
    this.getPlotList();
    this.threeAreasList();
  },
  methods: {
    getPlotList() {
      this.userId = this.user.userId
      listPlots(this.userId).then(res=>{
        this.plotList = res.data;
      })
    },
    aressChange() {
      this.queryParams.areasId = this.areasIds[this.areasIds.length - 1]
    },
    threeAreasList() {
      threeAreasList().then(data => {
        this.areasList = data.data
      })
    },
    /** 高级检索 */
    advancedSearch() {
      if (this.isAdvancedSearch == true){
        this.isAdvancedSearch = false
      }else if (this.isAdvancedSearch == false){
        this.isAdvancedSearch = true
      }
    },
    /** 查询实有单位ID列表 */
    getList() {
      this.loading = true;
      listUnits(this.queryParams).then(response => {
        this.unitsList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        unitPicture: null,
        unitName: null,
        categoryId: null,
        plotId: null,
        unitAddress: null,
        staffCount: null,
        contactPerson: null,
        contactPhone: null,
        idNumber: null,
        locationCoordinates: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {}
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实有单位ID";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUnits(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实有单位ID";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUnits(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnits(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实有单位ID编号为"' + ids + '"的数据项？').then(function() {
        return delUnits(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/units/export', {
        ...this.queryParams
      }, `units_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.custom-button {
  border: none; /* 移除边框 */
  background-color: transparent; /* 移除背景色 */
  color: inherit; /* 继承父元素颜色 */
  padding: 0; /* 移除内边距 */
}
</style>
